package unionfind;

public class unionfind1 {
	private int[] id;
	private int count;
	public unionfind1(int n){
		count=n;
		id = new int[n];
		for(int i=0;i<n;i++)
			id[i]=i;		
	}
	
	
	private int find(int p){
		assert p>0 && p<count;
		return  id[p];
	}
	
	public boolean isConnected(int p,int q)
	{	
		/*if(id[p]==id[q])
			return true;
		else
			return false;*/
		return id[p]==id[q];
		}
	
	public void unionElements(int p,int q){
		int pid=find(p);
		int qid=find(q);
		if(pid==qid)
			return;
		for(int i=0;i<count;i++){
			if(id[i]==pid)
				id[i]=qid;
		}
		
	
	
	}
	}
